<?php

declare(strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Kernel\Traits\DaoTrait;
use App\Model\Business;

use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Hyperf\DbConnection\Db;

/**
 * 商家DAO
 *
 * @author 李想(928674263@qq.com)
 * @package App\Dao
 */
class BusinessDao extends Dao
{
    use DaoTrait;

    public function model()
    {
        return Business::class;
    }

    /**
     * create
     * @param array $data
     * @return Builder|Model
     */
    public function created(array $data)
    {
        return Business::query()->create($data);
    }

    /**
     * 获取商家列表
     *
     * @param array $map 查询条件
     * @return mixed
     */
    public function BusinessReviewList(array $map = [])
    {
        return Business::query()
            ->where(function(Builder $query) use ($map) {
                return queryWhere($query, $map);
            })
            ->select('id', 'name', 'logo', 'tag', 'province', 'city', 'district', 'address', 'longitude', 'latitude', 'tel', 'full_discount_rate', 'enable_full_discount', 'enable_coupon', 'enable_voucher', 'created_at', 'legal_name', 'industry_id','status')
            ->paginate(10);
    }


    /**
     * 统计用户分享的会员为他带来积分收益的明细列表
     *
     * @param array $map
     * @param bool $abnormal 是否抛异常
     * @return mixed
     */
    public function get(array $map, bool $abnormal = true)
    {
        $result = Business::query()
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->select('id','logo','name','phone','tel','legal_name','created_at','province','city','district')
            ->orderByRaw("created_at desc,id desc")
            ->paginate(10);

        if (!$result && $abnormal === true) {
            $this->error('USER.NOT_FOUND');
        }

        return $result;
    }

    /**
     * 统计商品分享数量
     *
     * @param array $map
     * @return mixed
     */
    public function count(array $map = [])
    {
        return Business::query()
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->count();
    }

    /**
     * 查询商家
     *
     * @param array $map
     * @param array $select
     * @return mixed
     */
    public function first(array $map = [], array $select = ['*'])
    {
        return Business::query()
            ->where(function(Builder $query) use ($map) {
            return queryWhere($query, $map);
            })
            ->select($select)
            ->first();
    }

    /**
     * 查询商家 - 多条
     *
     * @param array $map
     * @param array $select
     * @return mixed
     */
    public function Many(array $map = [], array $select = ['*'])
    {
        return Business::query()
            ->where(function(Builder $query) use ($map) {
                return queryWhere($query, $map);
            })
            ->select($select)
            ->get();
    }

    /**
     * 获取商家列表
     *
     * @param array $map 查询条件
     * @param string $sort
     * @return mixed
     */
    public function getBusinessListIcon( string $sort = '', array $map = [])
    {
        return $sort ?

            Business::query()
                ->where(function(Builder $query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('status', 1)
                ->orderByRaw($sort)
                ->select('id', 'name', 'logo', 'tag', 'province', 'district', 'address', 'longitude', 'latitude', 'tel', 'full_discount_rate', 'enable_full_discount', 'enable_coupon', 'enable_voucher')
                ->get()
            :

            Business::query()
                ->where(function(Builder $query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('status', 1)
                ->orderByDesc('sort')
                ->orderBy('id')
                ->select('id', 'name', 'logo', 'tag', 'province', 'district', 'address', 'longitude', 'latitude', 'tel', 'full_discount_rate', 'enable_full_discount', 'enable_coupon', 'enable_voucher')
                ->get()
            ;
    }

    /**
     * 获取商家列表
     *
     * @param array $map 查询条件
     * @return mixed
     */
    public function getBusinessList( array $map = [])
    {
        return Business::query()
                ->where(function(Builder $query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('status', 1)
                ->orderByDesc('sort')
                ->orderBy('id')
                ->select('id', 'name', 'logo', 'tag', 'province', 'district', 'address', 'longitude', 'latitude', 'tel', 'full_discount_rate', 'enable_full_discount', 'enable_coupon', 'enable_voucher')
                ->get();
    }

    /**
     * 获取商家列表
     *
     * @param array $map 查询条件
     * @return mixed
     */
    public function getBusinessListPaging(array $map = [])
    {
        return Business::query()
            ->where(function(Builder $query) use ($map) {
                return queryWhere($query, $map);
            })
            ->where('status', 1)
            ->select('id', 'name', 'logo', 'tag', 'province', 'district', 'address', 'longitude', 'latitude', 'tel', 'full_discount_rate', 'enable_full_discount', 'enable_coupon', 'enable_voucher')
            ->paginate(10);
    }

    /**
     * 获取商家单条
     *
     * @param array $map 查询条件
     * @return mixed
     */
    public function getBusinessFirst(array $map = [])
    {
        return Business::query()
            ->where(function(Builder $query) use ($map) {
                return queryWhere($query, $map);
            })
            ->where('status', 1)
            ->orderByDesc('sort')
            ->orderBy('id')
            ->select('id', 'name', 'science', 'description','enable_coupon', 'enable_voucher', 'enable_full_discount' ,'forenoon_hours', 'afternoon_hours', 'hours_describe','logo', 'business_cover', 'province', 'city', 'district', 'address', 'longitude', 'latitude', 'tel', 'full_discount_rate')
            ->first();
    }

    /**
     * 通过商品ID判断商品是否存在
     *
     * @author 张昊(2396675557@qq.com)
     *
     * @param int $id
     * @return bool
     */
    public function existsBusinessById(int $id)
    {
        if ($id === 0) return false;

        return Business::query()->where('id', $id)->exists();
    }

    /**
     * 修改
     * @param array $map
     * @param array $data
     * @return bool
     */
    public function updateByIds(array $map, array $data)
    {
        return Business::query()
            ->where(function(Builder $query) use ($map) {
                return queryWhere($query, $map);
            })
            ->update($data);
    }

    /**
     * 根据查询条件获取所有的商家信息
     * @param array $map 对应关系
     * @param string $sort 排序
     * @param array $select 选择字段
     * @param int $paginate 每页页数
     * @return mixed
     */
    public function getBusinessByMap(array $map = [], string $sort = null,array $select = null, int $paginate = null)
    {
        $model = self::makeModel();

        if (isset($map['between_field'])) {
            foreach ($map['between_field'] as $k => $value) {
                $model = $model->whereBetween($k, $value);
            }
            unset($map['between_field']);
        }

        // 或
        if (isset($map['or_field'])) {
            $model = $model->where(function (Builder $query) use($map) {
                foreach ($map['or_field'] as $field => $item) {
                    // 普通查询
                    if (!is_array($item)) {
                        $query->orWhere($field, $item);
                        continue;
                    }
                    // 其它方式
                    switch (strtolower($item[0])) {
                        case 'in':
                            $query->orWhereIn($field, $item[1]);
                            break;

                        default:
                            $query->orWhere($field, $item[0], $item[1]);
                    }
                }
            });

            unset($map['or_field']);
        }
        
        if (!empty($map)) {
            $model = $model->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            });
        }
        if (null !== $sort) {
            $model = $model->orderByRaw($sort);
        }

        if (null !== $select) {
            $model = $model->select($select);
        }

        return $paginate > 0 ? $model->paginate($paginate) : $model->get();
    }

    /**
     * 通过parentID获得商家分组
     * @param array $ids
     * @param string $select_raw
     * @param $group_by
     * @return mixed
     */
    public function getSelectRawByIds(array $ids, string $select_raw, $group_by)
    {
        return Business::query()
            ->whereIn('parent_id', $ids)
            ->selectRaw($select_raw)
            ->groupBy($group_by)
            ->get();
    }

}